/**
 * Notes: 知识库控制器
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 * Date: 2024-03-10 19:20:00 
 */

const BaseProjectController = require('./base_project_controller.js');
const KnowledgeModel = require('../config/models_config').knowledge_model;
const timeUtil = require('../../../framework/helpers/time_util.js');

class KnowledgeController extends BaseProjectController {

    /** 获取知识库列表 */
    async getKnowledgeList() {
        // TODO: 实现获取知识库列表的逻辑
        return await this.success([]);
    }

    /** 获取知识库详情 */
    async getKnowledgeDetail() {
        // 确保数据库已初始化
        await this.initSetup();

        // 确保用户已登录
        if (!this._userId) {
            return this.createError('用户未登录');
        }

        // 获取请求参数
        let rules = {
            id: 'must|string'
        };

        // 取得数据
        let input = this.validateData(rules);

        let where = {
            _id: input.id,
            KNOWLEDGE_STATUS: ['in', [KnowledgeModel.STATUS.PUBLISHED, KnowledgeModel.STATUS.ARCHIVED]]
        };

        let fields = '*';

        let knowledge = await KnowledgeModel.getOne(where, fields);
        if (!knowledge) {
            return this.createError('该文档不存在或已被删除');
        }

        // 更新浏览次数
        await KnowledgeModel.inc(where, 'KNOWLEDGE_VIEW_COUNT', 1);
        await KnowledgeModel.edit(where, { KNOWLEDGE_LAST_VIEW_TIME: timeUtil.time() });

        return await this.success(knowledge);
    }

    /** 创建知识库文档 */
    async insertKnowledge() {
        // TODO: 实现创建知识库文档的逻辑
        return await this.success({});
    }

    /** 编辑知识库文档 */
    async editKnowledge() {
        // TODO: 实现编辑知识库文档的逻辑
        return await this.success({});
    }

    /** 删除知识库文档 */
    async delKnowledge() {
        // TODO: 实现删除知识库文档的逻辑
        return await this.success({});
    }

    /** 更新知识库文档状态 */
    async statusKnowledge() {
        // TODO: 实现更新知识库文档状态的逻辑
        return await this.success({});
    }
}

module.exports = KnowledgeController; 